/*
 * @(#)InstantMessageFrame.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */
package grupb.upf.gui;

/**
 * <p>This is the Instant Message Frame.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public class InstantMessageFrame extends javax.swing.JFrame {

    /**
     * My account.
     */
    private String me;
    /**
     * The opponent account.
     */
    private String to;
    /**
     * The opponent account.
     */
    private String toName;
    /**
     * the title of the Frame.
     */
    private String title;
    /**
     * The GUI which is controlling it, this frame will notice when it is
     * closed.
     */
    private P2Piki p2p;
    /**
     * Alt pressed (to detect alt+enter).
     */
    private boolean shiftPressed = false;

    /** Creates new form InstantMessageFrame */
    public InstantMessageFrame(final String me, final String to,
            final P2Piki p2piki) {
        this.me = me;
        this.to = to;
        this.toName = grupb.upf.util.Utilities.getUserName(to);
        this.p2p = p2piki;
        final StringBuffer sb = new StringBuffer(toName)
                .append(" conversation");
        title = sb.toString();
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * It will know if the user pressed ALT + ENTER or only ENTER
     * @return
     */
    private void enterReleased() {
        if (!shiftPressed) {
            sendMessage();
        }
    }
    /**
     * Method to add a response to the TextArea.
     * @param msg the message to add.
     */
    public void addResponse(final String msg) {
        // TODO add your handling code here:
        StringBuffer sb = new StringBuffer(toName)
                .append(": ").append(msg).append("\n");
        outputTextArea.append(sb.toString());
        jScrollPane2.getVerticalScrollBar().setValue(
                    jScrollPane2.getVerticalScrollBar().getMaximum());
    }
    /**
     * Method to get the opponent.
     * @return the opponent
     */
    public String getTo() {
        return this.to;
    }

    /**
     * Method to send a message.
     */
    public void sendMessage() {
        StringBuffer sb = new StringBuffer(me).append(": ").append(
                inputTextArea.getText()).append("\n");
        outputTextArea.append(sb.toString());
        p2p.sendInstantMessage(inputTextArea.getText(), to);
        inputTextArea.setText("");
        jScrollPane2.getVerticalScrollBar().setValue(
                    jScrollPane2.getVerticalScrollBar().getMaximum());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane2 = new javax.swing.JScrollPane();
        outputTextArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        inputTextArea = new javax.swing.JTextArea();
        sendMessageButton = new javax.swing.JButton();

        setTitle(title);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        outputTextArea.setColumns(20);
        outputTextArea.setEditable(false);
        outputTextArea.setRows(5);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, inputTextArea, org.jdesktop.beansbinding.ObjectProperty.create(), outputTextArea, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
        bindingGroup.addBinding(binding);

        jScrollPane2.setViewportView(outputTextArea);

        jPanel1.setMaximumSize(new java.awt.Dimension(3000, 71));
        jPanel1.setMinimumSize(new java.awt.Dimension(0, 71));
        jPanel1.setPreferredSize(new java.awt.Dimension(397, 71));

        inputTextArea.setColumns(20);
        inputTextArea.setRows(5);
        inputTextArea.setMaximumSize(new java.awt.Dimension(2147483647, 120));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, sendMessageButton, org.jdesktop.beansbinding.ObjectProperty.create(), inputTextArea, org.jdesktop.beansbinding.BeanProperty.create("nextFocusableComponent"));
        bindingGroup.addBinding(binding);

        inputTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inputTextAreaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                inputTextAreaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(inputTextArea);

        sendMessageButton.setText("Send");
        sendMessageButton.setMaximumSize(new java.awt.Dimension(69, 57));
        sendMessageButton.setMinimumSize(new java.awt.Dimension(69, 57));
        sendMessageButton.setPreferredSize(new java.awt.Dimension(69, 57));
        sendMessageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendMessageButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sendMessageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                    .addComponent(sendMessageButton, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendMessageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendMessageButtonActionPerformed
        // TODO add your handling code here:
        sendMessage();
}//GEN-LAST:event_sendMessageButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        p2p.removeIm(this);
    }//GEN-LAST:event_formWindowClosing

    private void inputTextAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputTextAreaKeyReleased
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            enterReleased();
        }
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_SHIFT) {
            shiftPressed = false;
        }
    }//GEN-LAST:event_inputTextAreaKeyReleased

    private void inputTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputTextAreaKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_SHIFT) {
            shiftPressed = true;
        }
    }//GEN-LAST:event_inputTextAreaKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea inputTextArea;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea outputTextArea;
    private javax.swing.JButton sendMessageButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
